<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser" size="primary"
        >+ 新增</el-button
      >
      <el-upload
        class="upload-demo"
        action="http://49.235.42.82:8888/api/pub/v2/credit_card/insert_creditCards"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="1"
        :on-success="onSuccess"
        :on-error="OnError"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button size="primary" type="success">批量导入</el-button>
      </el-upload>
      <common-form inline :formLabel="formLabel" :form="searchFrom">
        <el-button
          type="primary"
          @click="getList(searchFrom.keyword)"
          size="primary"
          >搜索</el-button
        >
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList()"
      @edit="editUser"
      @del="delUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm";
import CommonTable from "@/components/CommonTable";
import { creditCardSelectByPage } from "@/api/getData.js";
import { creditCardDeleteById } from "@/api/getData.js";
import { creditCardInsert } from "@/api/getData.js";
import { updateCreditCard } from "@/api/getData.js";
import {findByNameOrKey } from "@/api/getData.js";

export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: "party",
          label: "当事人",
          required: true,
        },
        {
          prop: "lawsuitNum",
          label: "诉讼案号",
          required: true,
        },
        {
          prop: "executeNum",
          label: "执行案号",
          required: true,
        },
        {
          prop: "sex",
          label: "性别",
          required: true,
        },
        {
          prop: "birthDate",
          label: "出生日期",
          required: true,
        },
        {
          prop: "nation",
          label: "民族",
          required: true,
        },
        {
          prop: "cardNum",
          label: "身份证号",
          required: true,
        },
        {
          prop: "tel",
          label: "联系方式",
          required: true,
        },
        {
          prop: "homeAddress",
          label: "户籍地址",
          required: true,
        },
        {
          prop: "currentAddress",
          label: "现住址",
          required: true,
        },
        {
          prop: "workAddress",
          label: "单位地址",
          required: true,
        },
        {
          prop: "principal",
          label: "本金",
          required: true,
        },
        {
          prop: "interest",
          label: "利息",
          required: true,
        },
        {
          prop: "amount",
          label: "标的额",
          required: true,
        },
        {
          prop: "litigationFee",
          label: "诉讼费",
          required: true,
        },
        {
          prop: "declaredFee",
          label: "公告费",
          required: true,
        },
        {
          prop: "lawyerFee",
          label: "律师费",
          required: true,
        },
        {
          prop: "creditCard",
          label: "卡号",
          required: true,
        },
        {
          prop: "deadlineDate",
          label: "截止日期",
          required: true,
        },
        {
          prop: "openDate",
          label: "开户日期",
          required: true,
        },
        {
          prop: "lastConsumeDate",
          label: "最后一次消费日期",
          required: true,
        },
        {
          prop: "lastPayDate",
          label: "最后一次还款日期",
          required: true,
        },
        {
          prop: "startOverDate",
          label: "开始逾期日",
          required: true,
        },
        {
          prop: "judgePrincipal",
          label: "判决本金",
          required: true,
        },
        {
          prop: "judgeInterest",
          label: "判决利息",
          required: true,
        },
        {
          prop: "courtLocation",
          label: "法院区域",
          required: true,
        },
        {
          prop: "paperSignDate",
          label: "文书落款日期",
          required: true,
        },
        {
          prop: "course",
          label: "案由",
          required: true,
        },
        {
          prop:"accuser",
          label:"原告",
          required:true
        },
        {
          prop:"accuserPeople",
          label:"原告负责人",
          required: true
        }
      ],
      config: {
        page: 1,
        total: 30,
        loading: false,
      },
      operateForm: {
        id: "",
        party: "",
        lawsuitNum: "",
        executeNum: "",
        sex: "",
        birthDate: "",
        nation: "",
        cardNum: "",
        tel: "",
        homeAddress: "",
        currentAddress: "",
        workAddress: "",
        principal: "",
        interest: "",
        amount: "",
        litigationFee: "",
        declaredFee: "",
        lawyerFee: "",
        creditCard: "",
        deadlineDate: "",
        openDate: "",
        lastPayDate: "",
        lastConsumeDate: "",
        startOverDate: "",
        judgePrincipal: "",
        judgeInterest: "",
        courtLocation: "",
        paperSignDate: "",
        course: "",
        accuser:"",
        accuserPeople:""
      },
      operateFormLabel: [
        {
          model: "party",
          label: "当事人",
        },
        {
          model: "lawsuitNum",
          label: "诉讼案号",
        },
        {
          model: "executeNum",
          label: "执行案号",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
        },
        {
          model: "birthDate",
          label: "出生日期",
          type: "date",
        },
        {
          model: "nation",
          label: "民族",
        },
        {
          model: "cardNum",
          label: "身份证号",
        },
        {
          model: "tel",
          label: "联系方式",
        },
        {
          model: "homeAddress",
          label: "户籍地址",
        },
        {
          model: "currentAddress",
          label: "现住址",
        },
        {
          model: "workAddress",
          label: "单位地址",
        },
        {
          model: "principal",
          label: "本金",
        },
        {
          model: "interest",
          label: "利息",
        },
        {
          model: "amount",
          label: "标的额",
        },
        {
          model: "litigationFee",
          label: "诉讼费",
        },
        {
          model: "declaredFee",
          label: "公告费",
        },
        {
          model: "lawyerFee",
          label: "律师费",
        },
        {
          model: "creditCard",
          label: "卡号",
        },
        {
          model: "deadlineDate",
          label: "截止日期",
          type: "date",
        },
        {
          model: "openDate",
          label: "开户日期",
          type: "date",
        },
        {
          model: "lastConsumeDate",
          label: "最后一次消费日期",
          type: "date",
        },
        {
          model: "lastPayDate",
          label: "最后一次消费日期",
          type: "date",
        },
        {
          model: "startOverDate",
          label: "开始逾期日期",
          type: "date",
        },
        {
          model: "judgePrincipal",
          label: "判决本金",
        },
        {
          model: "judgeInterest",
          label: "判决利息",
        },
        {
          model: "courtLocation",
          label: "法院区域",
        },
        {
          model: "paperSignDate",
          label: "文书落款日期",
          type: "date",
        },
        {
          model: "course",
          label: "案由",
        },
        {
          model:"accuser",
          label:"原告"
        },
        {
          model:"accuserPeople",
          label:"原告负责人"
        }
      ],
      searchFrom: {
        keyword: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
        },
      ],
    };
  },
  methods: {
      onSuccess()
    {
          this.$message({
          message: '信息录入成功，请刷新页面',
          type: 'success'
        });

    },
    OnError()
    {
      this.$message({
          message: '信息录入失败，请检查日期格式,正确格式为2021/1/19',
          type: 'error'
        });

    },
    async getList(name) {
      this.config.loading = false;
      // 搜索时，页码需要设置为1，才能正确返回数据，因为数据是从第一页开始返回的
      name ? (this.config.page = 1) : "";
      if(this.searchFrom.keyword=="")
      {
       const result = await creditCardSelectByPage(this.config.page);
      console.log(result);
      if (result.data.code == 0) {
        this.tableData = result.data.data.creditCards;
        this.config.total = result.data.data.count;
        this.config.loading = false;
      }
        
      }
      else
      {

             console.log(name);
      const result = await findByNameOrKey(this.searchFrom.keyword,this.config.page);
      console.log(result);

        this.tableData = result.data.data.creditCards;
        this.config.total = result.data.data.count;
      this.config.loading = false;

      }

    },
    async SelectByName(name) {
      name ? (this.config.page = 1) : "";
      console.log(name);
      const result = await findByNameOrKey(name,this.config.page);
      console.log(result);

        this.tableData = result.data.data.creditCards;
        this.config.total = result.data.data.count;
      this.config.loading = false;
    },
    addUser() {
      this.operateForm = {};
      this.operateType = "add";
      this.isShow = true;
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    confirm() {
      if (this.operateType === "edit") {
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");
        updateCreditCard(
          //       id, party, lawsuit_num, execute_num, sex, birth_date,
          // nation, card_num, tel, home_address, current_address,
          // work_address, principal, interest, amount, litigation_fee,
          // declared_fee, lawyer_fee, credit_card, deadline_date, open_date,
          // last_consume_date, last_pay_date, start_over_date, judge_principal, judge_interest,
          // court_location, paper_sign_date, course

          this.operateForm.id,
          this.operateForm.party,
          this.operateForm.lawsuitNum,
          this.operateForm.executeNum,
          this.operateForm.sex,
          this.operateForm.birthDate,
          this.operateForm.nation,
          this.operateForm.cardNum,
          this.operateForm.tel,
          this.operateForm.homeAddress,

          this.operateForm.currentAddress,
          this.operateForm.workAddress,

          this.operateForm.principal,
          this.operateForm.interest,
          this.operateForm.amount,
          this.operateForm.litigationFee,
          this.operateForm.declaredFee,
          this.operateForm.lawyerFee,
          this.operateForm.creditCard,
          this.operateForm.deadlineDate,
          this.operateForm.openDate,
          this.operateForm.lastConsumeDate,
          this.operateForm.lastPayDate,
          this.operateForm.startOverDate,
          this.operateForm.judgePrincipal,
          this.operateForm.judgeInterest,
          this.operateForm.courtLocation,
          this.operateForm.paperSignDate,
          this.operateForm.course,
          this.operateForm.accuser,
          this.operateForm.accuserPeople
        ).then((res) => {
          (this.isShow = false),
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
          this.getList();
        });
      } else {
        // this.$http.post('/api/user/add', this.operateForm).then(res => {
        console.log(this.operateForm);
        // name, sex, nature, address, card_num, lawyer_id
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");
        creditCardInsert(
          this.operateForm.party,
          this.operateForm.lawsuitNum,
          this.operateForm.executeNum,
          this.operateForm.sex,
          this.operateForm.birthDate,
          this.operateForm.nation,
          this.operateForm.cardNum,
          this.operateForm.tel,
          this.operateForm.homeAddress,
          this.operateForm.currentAddress,
          this.operateForm.workAddress,
          this.operateForm.principal,
          this.operateForm.interest,
          this.operateForm.amount,
          this.operateForm.litigationFee,
          this.operateForm.declaredFee,
          this.operateForm.lawyerFee,
          this.operateForm.creditCard,
          this.operateForm.deadlineDate,
          this.operateForm.openDate,
          this.operateForm.lastConsumeDate,
          this.operateForm.lastPayDate,
          this.operateForm.startOverDate,
          this.operateForm.judgePrincipal,
          this.operateForm.judgeInterest,
          this.operateForm.courtLocation,
          this.operateForm.paperSignDate,
          this.operateForm.course,
          this.operateForm.accuser,
          this.operateForm.accuserPeople
     
        ).then((res) => {
          if (res.data.code === 0) {
            console.log("插入成功");
            this.$message({
              type: "success",
              message: "添加成功!",
            });
          } else {
            console.log("插入失败");
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }
          (this.isShow = false), this.getList();
        });
      }
    },

    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = row.id;
          // this.$http
          //   .get('/api/v1/pub/defendant_one/deleteById', {
          //     params: {
          //       id
          //     }
          //   })
          const result = creditCardDeleteById(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>


<style lang="scss" scoped>
.manage {
  height: 90%;
  padding-bottom: 20px;
  padding-top: 20px;
  overflow: hidden;
  &-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }
}
</style>